package mic.hsacnu.breathingmosmc;

import java.util.ArrayList;

import mic.hsacnu.data.StaticDatas;
import mic.hsacnu.data.TouchType;
import mic.hsacnu.debug.TestDatas;
import mic.hsacnu.util.CustomToast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button key_button;
	private long last_time;
	public boolean any_operation=false;
	private ReadOperationThread roThread=new ReadOperationThread();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		key_button=(Button) findViewById(R.id.key_button);
		last_time=System.currentTimeMillis();
		key_button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				any_operation=true;
				StaticDatas.current_morse.add(TouchType.short_touch);
			}
		});
		key_button.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				any_operation=true;
				StaticDatas.current_morse.add(TouchType.long_touch);
				return true;
			}
		});
		StaticDatas.h=new Handler();
		TestDatas.getTestDatas();
		roThread.start();
	}
	
	public void setting(View source){
		//==========在此处编写关于设置的代码==========//
		
	}
	
	public void copyright(View source){
		//==========在此处编写关于启动关于窗体的代码==========//
		startActivity(new Intent(getApplicationContext(), CopyRightActivity.class));
	}
	
	private void execOperation(final String name, ArrayList<Enum<TouchType>> morse){
		//==========在此处编写关于执行指定动作的代码==========//
		Log.i("MainActivity", "Morse操作识别成功，被执行的指令是："+name);
		StaticDatas.h.post(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				CustomToast.makeText(getApplicationContext(), "Morse操作识别成功，执行的动作是："+name, Toast.LENGTH_LONG);
			}
		});
	}
	
	private void execSucces(final String name, ArrayList<Enum<TouchType>> morse){
		//==========在此处编写关于执行指定动作成功后的代码==========//
		Log.i("MainActivity", "Morse操作识别成功，指令被成功执行："+name);
		StaticDatas.h.post(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				CustomToast.makeText(getApplicationContext(), "Morse操作识别成功，指令被成功执行："+name, Toast.LENGTH_LONG);
			}
		});
	}
	
	private void cannotFindMorse(ArrayList<Enum<TouchType>> morse){
		//==========在此处编写关于没有找到手势对应的动作的代码==========//
		Log.i("MainActivity", "Morse无法被识别！");
		StaticDatas.h.post(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				CustomToast.makeText(getApplicationContext(), "Morse操作识别失败！", Toast.LENGTH_LONG);
			}
		});
	}
	
	public class ReadOperationThread extends Thread{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(true){
				if(any_operation==true && StaticDatas.current_morse.size()!=0){
					any_operation=false;
					try {
						Log.v("Thread", "延时开始！");
						Thread.sleep(StaticDatas.wait_time);
						Log.v("Thread", "延时结束！");
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						Log.w("ReadOperationThread", "线程休眠时出现错误！");
					}
				}else if(any_operation==false && StaticDatas.current_morse.size()!=0){
					Log.v("Thread", "开始查找字典！");
					boolean exec_flag=false;
					String exec_name=null;
					ArrayList<Enum<TouchType>> temp_morse=StaticDatas.current_morse;
					for(int i=0;i<StaticDatas.dictionary.size();++i){
						if(StaticDatas.dictionary.get(i).content.equals(StaticDatas.current_morse)==true){
							execOperation(StaticDatas.dictionary.get(i).name, temp_morse);
							exec_flag=true;
							exec_name=StaticDatas.dictionary.get(i).name;
							break;
						}
					}
					if(exec_flag==true){
						StaticDatas.current_morse.clear();
						execSucces(exec_name, temp_morse);
					}else{
						cannotFindMorse(temp_morse);
					}
					StaticDatas.current_morse.clear();
				}
			}
		}

		public ReadOperationThread() {
			super();
			// TODO Auto-generated constructor stub
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
